﻿@{
    ViewBag.Title = "Index";
}
<link href="../../Scripts/JQuery-zTree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet" />
<script src="../../Scripts/JQuery-zTree/js/jquery-1.4.4.min.js"></script>
<script src="../../Scripts/JQuery-zTree/js/jquery.ztree.all-3.5.min.js"></script>
<style type="text/css">
    .ztree li span.button.add { margin-left: 2px; margin-right: -1px; background-position: -144px 0; vertical-align: top; *vertical-align: middle; }
    a { text-decoration: underline; }
</style>
<script type="text/javascript">
    /*
   动态数据测试部分开始 */
    var log, className = "dark";
    var treeID = "treeDemo";
    var curDragNodes, autoExpandNode;
    var newCount = 1;

    //异步加载节点
    var setting4 = {
        data: {
            simpleData: {
                enable: true,
                idKey: "id",
                pIdKey: "pId",
                rootPId: null
            }
        },
        edit: {
            enable: true,
            editNameSelectAll: true,
            showRemoveBtn: showRemoveBtn,
            showRenameBtn: showRenameBtn,
            removeTitle: "删除",
            renameTitle: "编辑",
            drag: {
                autoExpandTrigger: true,
                prev: dropPrev,
                inner: dropInner,
                next: dropNext
            }
        },
        view: {
            addHoverDom: addHoverDom,
            removeHoverDom: removeHoverDom,
            selectedMulti: false
        },
        async: {
            //异步加载
            enable: true,
            url: "/ZTree/AsyncGetNodes",
            autoParam: ["id", "name", "pId"]
        },
        callback: {
            /*节点获取与展开回调*/
            beforeExpand: beforeExpand,
            onAsyncSuccess: onAsyncSuccess,
            onAsyncError: onAsyncError,
            /*编辑与删除回调*/
            beforeEditName: beforeEditName,
            beforeRemove: beforeRemove,
            beforeRename: beforeRename,
            onRemove: onRemove,
            onRename: onRename,
            /*拖动回调*/
            beforeDrag: beforeDrag,
            beforeDrop: beforeDrop,
            beforeDragOpen: beforeDragOpen,
            onDrag: onDrag,
            onDrop: onDrop,
            onExpand: onExpand
        }
    };
    function beforeEditName(treeId, treeNode) {
        className = (className === "dark" ? "" : "dark");
        var zTree = $.fn.zTree.getZTreeObj(treeID);
        zTree.selectNode(treeNode);
        return confirm("进入节点 -- " + treeNode.name + " 的编辑状态吗？");
    }
    function beforeRemove(treeId, treeNode) {
        className = (className === "dark" ? "" : "dark");
        var zTree = $.fn.zTree.getZTreeObj(treeID);
        zTree.selectNode(treeNode);
        return confirm("确认删除 节点 -- " + treeNode.name + " 吗？");
    }
    function beforeRename(treeId, treeNode, newName, isCancel) {
        className = (className === "dark" ? "" : "dark");
        if (newName.length == 0) {
            alert("节点名称不能为空.");
            var zTree = $.fn.zTree.getZTreeObj(treeID);
            setTimeout(function () { zTree.editName(treeNode) }, 10);
            return false;
        }
        return true;
    }
    function onRemove(e, treeId, treeNode) {
        $.ajax({
            url: '/ZTree/AsyncDelNodes', //url  action是方法的名称
            data: { id: treeNode.id },
            type: 'POST',
            success: function (data) {
                alert(data);
            }
        });

    }
    function onRename(e, treeId, treeNode, isCancel) {
        alert("编辑了节点" + treeNode.id + " " + treeNode.name);
        $.ajax({
            url: '/ZTree/AsyncEditNodes', //url  action是方法的名称
            data: { id: treeNode.id, name: treeNode.name },
            type: 'POST',
            success: function (data) {
                alert(data);
            }
        });

    }
    function showRemoveBtn(treeId, treeNode) {
        //return !treeNode.isFirstNode;
        return true;
    }
    function showRenameBtn(treeId, treeNode) {
        // return treeNode.isLastNode;
        return true;
    }
    function addHoverDom(treeId, treeNode) {
        var sObj = $("#" + treeNode.tId + "_span");
        if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
        var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
            + "' title='add node' onfocus='this.blur();'></span>";
        sObj.after(addStr);
        var btn = $("#addBtn_" + treeNode.tId);
        if (btn) btn.bind("click", function () {
            var zTree = $.fn.zTree.getZTreeObj(treeID);
            zTree.addNodes(treeNode, {
                id: (100 + newCount), pId: treeNode.id, name: "new node" + newCount
            });
            $.ajax({
                url: '/ZTree/AsyncAddNodes', //url  action是方法的名称
                data: { id: (100 + newCount), pid: treeNode.id, name: "new node" + newCount },
                type: 'POST',
                success: function (data) {
                    //异常完成后，刷新父节点及下面所有子节点
                    zTree.reAsyncChildNodes(treeNode, "refresh");
                }
            });
            newCount++;
            return false;
        });
    };
    function removeHoverDom(treeId, treeNode) {
        $("#addBtn_" + treeNode.tId).unbind().remove();
    };
    function createTree() {
        $.ajax({
            url: '/ZTree/AsyncGetNodes', //url  action是方法的名称
            data: { id: 0 },
            type: 'Get',
            dataType: "text", //可以是text，如果用text，返回的结果为字符串；如果需要json格式的，可是设置为json
            success: function (data) {
                $.fn.zTree.init($("#" + treeID), setting4, eval('(' + data + ')'));
            },
            error: function (msg) {
                alert(" 数据加载失败！" + msg);
            }
        });
    }
    function beforeExpand(treeId, treeNode) {
        if (!treeNode.isAjaxing) {
            return true;
        } else {
            alert("zTree 正在下载数据中，请稍后展开节点。。。");
            return false;
        }
    }
    function onAsyncSuccess(event, treeId, treeNode, msg) {

    }
    function onAsyncError() {
        alert(" 数据加载失败");
    }
    $(document).ready(function () {
        createTree();
    });



    function dropPrev(treeId, nodes, targetNode) {

        var pNode = targetNode.getParentNode();
        if (pNode && pNode.dropInner === false) {
            return false;
        } else {
            for (var i = 0, l = curDragNodes.length; i < l; i++) {
                var curPNode = curDragNodes[i].getParentNode();
                if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
                    return false;
                }
            }
        }
        return true;
    }
    function dropInner(treeId, nodes, targetNode) {

        if (targetNode && targetNode.dropInner === false) {
            return false;
        } else {
            for (var i = 0, l = curDragNodes.length; i < l; i++) {
                if (!targetNode && curDragNodes[i].dropRoot === false) {
                    return false;
                } else if (curDragNodes[i].parentTId && curDragNodes[i].getParentNode() !== targetNode && curDragNodes[i].getParentNode().childOuter === false) {
                    return false;
                }
            }
        }
        return true;
    }
    function dropNext(treeId, nodes, targetNode) {

        var pNode = targetNode.getParentNode();
        if (pNode && pNode.dropInner === false) {
            return false;
        } else {
            for (var i = 0, l = curDragNodes.length; i < l; i++) {
                var curPNode = curDragNodes[i].getParentNode();
                if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
                    return false;
                }
            }
        }
        return true;
    }
    function beforeDrag(treeId, treeNodes) {
        className = (className === "dark" ? "" : "dark");
        for (var i = 0, l = treeNodes.length; i < l; i++) {
            if (treeNodes[i].drag === false) {
                curDragNodes = null;
                return false;
            } else if (treeNodes[i].parentTId && treeNodes[i].getParentNode().childDrag === false) {
                curDragNodes = null;
                return false;
            }
        }
        curDragNodes = treeNodes;
        return true;
    }
    function beforeDragOpen(treeId, treeNode) {
        autoExpandNode = treeNode;
        return true;
    }
    function beforeDrop(treeId, treeNodes, targetNode, moveType, isCopy) {
        className = (className === "dark" ? "" : "dark");
        return true;
    }
    function onDrag(event, treeId, treeNodes) {
        className = (className === "dark" ? "" : "dark");
    }
    function onDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {
        className = (className === "dark" ? "" : "dark");
        $.ajax({
            url: '/ZTree/AsyncDragNodes', // url  action是方法的名称
            data: { id: treeNodes[0].id, pid: targetNode.id, name: treeNodes[0].name, moveType: moveType, isCopy: isCopy },
            type: 'POST',
            success: function (data) {
            }
        });

    }
    function onExpand(event, treeId, treeNode) {
        if (treeNode === autoExpandNode) {
            className = (className === "dark" ? "" : "dark");
        }
    }

    function showLog(msg) {
        $("#logger").append(msg);
    }
</script>
<div class="zTreeDemoBackground left">
    <ul id="treeDemo" class="ztree"></ul>

    <div id="logger"></div>
    <input type="text" id="treenode" />

    @foreach (var item in ViewBag.List as List<Domain.Entities.Category>)
    { 
        <p>@item.ID</p>
    }

</div>
